package com.example.firstapp.service

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.util.Log
import com.example.firstapp.R
import com.example.firstapp.databinding.ActivityServiceBinding
import kotlin.concurrent.thread

class ServiceActivity : AppCompatActivity() {
    lateinit var downloadBinder: MyService.DownloadBinder
    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            downloadBinder = service as MyService.DownloadBinder
            downloadBinder.startDownload()
            downloadBinder.getProgress()
        }
        override fun onServiceDisconnected(name: ComponentName) {
        }
    }
    lateinit var binding:ActivityServiceBinding
    val updateText = 1
    lateinit var handler2:Handler
    // 1、创建一个hanler实例，接收信息
    //            object:Handler(looper实例) 这里绑定的是主线程的loop
    val handler = object:Handler(Looper.getMainLooper()){
        // looper会调用loop()，循环从MessageQueue获取消息
        // 当接收到信息时，就可以从MessageQueue获取消息，然后执行以下方法
        override fun handleMessage(msg: Message) {
            when(msg.what){
                updateText -> binding.text1.text = "更改后的文字"
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_service)

        binding = ActivityServiceBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.sv1.setOnClickListener {
            thread {
                // 2、创建一个message对象，并通过调用hanler发出去
                val msg = Message()
                msg.what = updateText
                handler.sendMessage(msg)
            }
        }
        binding.sv2.setOnClickListener {
            // 创建 Handler 并关联当前线程的 Looper
            handler2 = object : Handler(Looper.myLooper()!!)  {
                override fun handleMessage(msg: Message) {
                    // 在此处理来自其他线程的消息
                    println("收到线程${msg.what}的消息")
                }
            }
        }
        binding.sv3.setOnClickListener {
            thread {
                // 2、创建一个message对象，并通过调用hanler发出去
                val msg3 = Message()
                msg3.what = 3
                handler2.sendMessage(msg3)
            }
        }
        binding.startSV.setOnClickListener {
            val intent = Intent(this, MyService::class.java)
            startService(intent)
        }
        binding.stopSV.setOnClickListener {
            val intent = Intent(this, MyService::class.java)
            stopService(intent)
        }
        binding.bindSV.setOnClickListener {
            val intent = Intent(this, MyService::class.java)
            startService(intent)
        }
        binding.unbindSV.setOnClickListener {
            unbindService(connection) // 解绑Service
        }
        binding.startQiantai.setOnClickListener {
            val intent = Intent(this, QiantaiService::class.java)
            println(1)
            startService(intent)
        }
        binding.intentservice.setOnClickListener {
            val intent = Intent(this, MyIntentService::class.java)
            startService(intent)
        }
    }
}